﻿using CodeCity.SDK;
using CodeCity.UserManagement.Model;
using CodeCity.UserManagement.Service.ApplicationModel;
using CodeCity.UserMangament.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeCity.UserManagement.Service
{
    public class HumanService
    {
        private readonly HumanRepository humanRepository;

        public HumanService(HumanRepository humanRepository)
        {
            this.humanRepository = humanRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="humanRegisterModel"></param>
        /// <returns></returns>
        public async Task RegisterAsync(HumanRegisterModel humanRegisterModel)
        {
            if (await humanRepository.ExistAsync(humanRegisterModel.Id))
            {
                throw new OperationException("该市民已注册");
            }
            var human = humanRepository.Insert(new Human()
            {
                BirthDay = humanRegisterModel.BirthDay,
                FullName = humanRegisterModel.Name,
                Gender = humanRegisterModel.Gender,
                HomeBuildingId = humanRegisterModel.HomeBuildingId,
                HosiptalBuildingId = humanRegisterModel.HosiptalBuildingId,
                IsAtHome = humanRegisterModel.IsAtHome,
                IsBusy = humanRegisterModel.IsBusy,
                SpouseHumanId = humanRegisterModel.SpouseHumanId,
                FatherId = humanRegisterModel.FatherId,
                MotherId = humanRegisterModel.MotherId,
                Status = humanRegisterModel.Status,
                UnitId = humanRegisterModel.Id,
                WorkingBuildingId = humanRegisterModel.WorkingBuildingId
            });
            await humanRepository.SaveChangesAsync();
        }

        public async Task UpdateStatusAsync(UpdateStatusModel model)
        {
            var human = await humanRepository.GetByUnitIdAsync(model.Id);
            if (human == null)
            {
                throw new OperationException("HumanId:" + model.Id + "不存在");
            }
            human.Status = model.Status;
            human.CurrentBuildingId = model.CurrentBuildingId;
            human.IsBusy = model.IsBusy;
            await humanRepository.SaveChangesAsync();
        }

        public async Task ResignWorkAsync(HumanWorkModel model)
        {
            var human = await humanRepository.GetByUnitIdAsync(model.HumanId);
            if (human == null)
            {
                throw new OperationException("HumanId:" + model.HumanId + "不存在");
            }
            human.WorkingBuildingId = 0;
            await humanRepository.SaveChangesAsync();
        }

        public async Task CheckinWorkAsync(HumanWorkModel model)
        {
            var human = await humanRepository.GetByUnitIdAsync(model.HumanId);
            if (human == null)
            {
                throw new OperationException("HumanId:" + model.HumanId + "不存在");
            }
            human.WorkingBuildingId = model.BuildingId;
            await humanRepository.SaveChangesAsync();
        }

        public Task<List<Human>> LoadAllAsync()
        {
            return humanRepository.LoadALlAsync();
        }
    }
}
